//
//  SwfitLabelTableViewCell.swift
//  XMenSwift
//
//  Created by X-MEN on 2022/4/14.
//

import UIKit

class SwiftLabelTableViewCell: UITableViewCell {
    
    let itemW = 80.0
    
    var collectionView : UICollectionView!
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        configUi()
        addNotification()
        
    }
    
    var data: String? {
        didSet {
            self.collectionView.reloadData()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configUi() {
        let layout = UICollectionViewFlowLayout()
        // 设置滚动的方向
        layout.scrollDirection = .horizontal;
        // 行间距
        layout.minimumLineSpacing = 1
        // 设置cell之间的间距
        layout.minimumInteritemSpacing = 1
        collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.setCollectionViewLayout(layout, animated: false)
        collectionView.backgroundColor = UIColor.clear
        collectionView.showsHorizontalScrollIndicator = false;
        collectionView.showsVerticalScrollIndicator = false;
        collectionView.isPagingEnabled = false;
        collectionView.alwaysBounceHorizontal = true;
        collectionView.dataSource = self;
        collectionView.delegate = self;
        collectionView.register(SIAccountSocialAccountDisconnectCell.self, forCellWithReuseIdentifier: NSStringFromClass(SIAccountSocialAccountDisconnectCell.self))
        self.contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    func addNotification() {
        NotificationCenter.default.addObserver(self, selector: #selector(dataRefresh(_:)), name: Notification.Name("KUpdateWalletNotification"), object: nil)
    }
    
    @objc func dataRefresh(_ noti: Notification) {
        if let object = noti.object as? CGPoint {
            if self.collectionView.contentOffset.x != object.x {
                self.collectionView.setContentOffset(object, animated: false)
            }
        }
        
    }
    
}


extension SwiftLabelTableViewCell: UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        NotificationCenter.default.post(name: Notification.Name("KUpdateWalletNotification"), object: scrollView.contentOffset)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1000
//        return randomIn(min: 30, max: 1000)
    }
    
    ///随机数
    func randomIn(min: Int, max: Int) -> Int {
        return Int(arc4random()) % (max - min + 1) + min
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(SIAccountSocialAccountDisconnectCell.self), for: indexPath) as? SIAccountSocialAccountDisconnectCell {
            cell.iconImageV.image = UIImage(named: "bigXiong")
            cell.titleLabel.text = "\(indexPath.row)"
            return cell
        }
        return UICollectionViewCell()
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.reloadData()
    }
    
    //返回item高度的方法
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        //找出最大的数组数
        return CGSize(width: itemW, height:itemW)
    }
    
}

